$(function () {
    $('#day').datetimepicker({
        format: 'yyyy-MM-dd',
        language:  'zh-CN',
        minView: 'days'
    });
});

var app = angular.module('scheduleList', []).controller('listController', function ($scope, $http) {
    if (sessionStorage.getItem('schedule.stime') != null && sessionStorage.getItem('schedule.stime') != "") {
        $scope.startTime = sessionStorage.getItem('schedule.stime');
        $scope.endTime = sessionStorage.getItem('schedule.etime');

        $('#selectday').val($scope.startTime.substr(0, 10));
    } else {
        var date = moment().weekday(0);

        $scope.startTime = date.format('YYYY-MM-DD 00:00:00');
        $scope.endTime = date.add(6, 'days').format('YYYY-MM-DD 23:59:59');
        //$scope.startTime = date.format('YYYY-MM-DD 00:00:00');
        //$scope.endTime = date.format('YYYY-MM-DD 23:59:59');
        $('#selectday').val(date.format('yyyy-MM-dd'));
    }

    $scope.queryOneDay = function (date) {
        sessionStorage.setItem('schedule.stime', $scope.startTime);
        sessionStorage.setItem('schedule.etime', $scope.endTime);
        $scope.listData = [];
        var url = '/smtool_project/api/schedule/getSellerActivity?';
            url += 'startTime='+date.format('YYYY-MM-DD 00:00:00');
            url += '&finishTime=' + date.format('YYYY-MM-DD 23:59:59');

        $.AMUI.progress.start();
        $http.get(url).success(function (response) {
            if (response.code == 200) {
                $scope.listData = response;

            } else {
                $scope.listData = [];
            }
            $.AMUI.progress.done();
        }).error(function (err) {
            $scope.listData = [];
            $.AMUI.progress.done();
        })
    };
    $scope.queryList = function () {
        sessionStorage.setItem('schedule.stime', $scope.startTime);
        sessionStorage.setItem('schedule.etime', $scope.endTime);
        $scope.listData = [];
        var url = '/smtool_project/api/schedule/getSellerActivity?';
        if($scope.startTime != '' && $scope.endTime != '') {
            url += 'startTime=' + $scope.startTime;
            url += '&finishTime=' + $scope.endTime;
        }
        $.AMUI.progress.start();
        $http.get(url).success(function (response) {
            if (response.code == 200) {
                //$scope.listData = response;

                var data={};
                data.roadshowResult=[];
                data.strategymeetingResult=[];
                data.teleConferenceResult=[];
                data.serveyingResult=[];
                data.workDiaryResult=[];

                var nowDay = moment($('#calendar').fullCalendar('getDate').format('YYYY-MM-DD'));
                for(o in response.roadshowResult) {
                    var start = moment(response.roadshowResult[o].startTime, 'YYYY/MM/DD hh:mm');
                    var finish = moment(response.roadshowResult[o].finishTime, 'YYYY/MM/DD hh:mm');
                    if (nowDay.isBetween(start, finish, 'day') || nowDay.isSame(start, 'day') || nowDay.isSame(finish, 'day')) {
                        data.roadshowResult.push(response.roadshowResult[o]);
                    }
                }
                for(o in response.strategymeetingResult) {
                    var start = moment(response.strategymeetingResult[o].startTime, 'YYYY/MM/DD hh:mm');
                    var finish = moment(response.strategymeetingResult[o].finishTime, 'YYYY/MM/DD hh:mm');
                    if (nowDay.isBetween(start, finish, 'day') || nowDay.isSame(start, 'day') || nowDay.isSame(finish, 'day')) {
                        data.strategymeetingResult.push(response.strategymeetingResult[o]);
                    }
                }
                for(o in response.teleConferenceResult) {
                    var start = moment(response.teleConferenceResult[o].startTime, 'YYYY/MM/DD hh:mm');
                    var finish = moment(response.teleConferenceResult[o].finishTime, 'YYYY/MM/DD hh:mm');
                    if (nowDay.isBetween(start, finish, 'day') || nowDay.isSame(start, 'day') || nowDay.isSame(finish, 'day')) {
                        data.teleConferenceResult.push(response.teleConferenceResult[o]);
                    }
                }
                for(o in response.serveyingResult) {
                    var start = moment(response.serveyingResult[o].startTime, 'YYYY/MM/DD hh:mm');
                    var finish = moment(response.serveyingResult[o].finishTime, 'YYYY/MM/DD hh:mm');
                    if (nowDay.isBetween(start, finish, 'day') || nowDay.isSame(start, 'day') || nowDay.isSame(finish, 'day')) {
                        data.serveyingResult.push(response.serveyingResult[o]);
                    }
                }
                for(o in response.workDiaryResult) {
                    var start = moment(response.workDiaryResult[o].startTime, 'YYYY/MM/DD hh:mm');
                    var finish = moment(response.workDiaryResult[o].finishTime, 'YYYY/MM/DD hh:mm');
                    if (nowDay.isBetween(start, finish, 'day') || nowDay.isSame(start, 'day') || nowDay.isSame(finish, 'day')) {
                        data.workDiaryResult.push(response.workDiaryResult[o]);
                    }
                }

                $scope.listData =data;

                var date = $('#calendar').fullCalendar('getDate').weekday(-1);
                var event = [];

                for(var i = 0; i < 7; i++) {
                    var count = 0;
                    var day = moment(date.add(1, 'days').format('YYYY-MM-DD'));
                    for(o in response.roadshowResult) {
                        var start = moment(response.roadshowResult[o].startTime, 'YYYY/MM/DD hh:mm').utc();
                        var finish = moment(response.roadshowResult[o].finishTime, 'YYYY/MM/DD hh:mm').utc();
                        if (day.isBetween(start, finish, 'day') || day.isSame(start, 'day') || day.isSame(finish, 'day')) {
                            count ++;
                        }
                    }
                    for(o in response.strategymeetingResult) {
                        var start = moment(response.strategymeetingResult[o].startTime, 'YYYY/MM/DD hh:mm').utc();
                        var finish = moment(response.strategymeetingResult[o].finishTime, 'YYYY/MM/DD hh:mm').utc();
                        if (day.isBetween(start, finish, 'day') || day.isSame(start, 'day') || day.isSame(finish, 'day')) {
                            count ++;
                        }
                    }
                    for(o in response.teleConferenceResult) {
                        var start = moment(response.teleConferenceResult[o].startTime, 'YYYY/MM/DD hh:mm').utc();
                        var finish = moment(response.teleConferenceResult[o].finishTime, 'YYYY/MM/DD hh:mm').utc();
                        if (day.isBetween(start, finish, 'day') || day.isSame(start, 'day') || day.isSame(finish, 'day')) {
                            count ++;
                        }
                    }
                    for(o in response.serveyingResult) {
                        var start = moment(response.serveyingResult[o].startTime, 'YYYY/MM/DD hh:mm').utc();
                        var finish = moment(response.serveyingResult[o].finishTime, 'YYYY/MM/DD hh:mm').utc();
                        if (day.isBetween(start, finish, 'day') || day.isSame(start, 'day') || day.isSame(finish, 'day')) {
                            count ++;
                        }
                    }
                    for(o in response.workDiaryResult) {
                        var start = moment(response.workDiaryResult[o].startTime, 'YYYY/MM/DD hh:mm').utc();
                        var finish = moment(response.workDiaryResult[o].finishTime, 'YYYY/MM/DD hh:mm').utc();
                        if (day.isBetween(start, finish, 'day') || day.isSame(start, 'day') || day.isSame(finish, 'day')) {
                            count ++;
                        }
                    }
                    if(count != 0) {
                        event.push({title: count,start: day.format('YYYY-MM-DD')});
                    }
                }

                //添加日历事项
                $('#calendar').fullCalendar('addEventSource', {
                    events: event
                });
            } else {
                $scope.listData = [];
            }
            $.AMUI.progress.done();
        }).error(function (err) {
            $scope.listData = [];
            $.AMUI.progress.done();
        })
    };

    $scope.formatDate = function (datestr, format) {
        var date = new Date(datestr);
        return date.format(format);
    };


    // $('#day').datetimepicker({
    //         format: 'yyyy-MM-dd',
    //         language:  'zh-CN',
    //         minView: 'days'
    //     }).on('changeDay', function(event) {
    //     $scope.startTime = event.date.format('yyyy-MM-dd 00:00:00');
    //     $scope.endTime = event.date.format('yyyy-MM-dd 23:59:59');
    //     $(this).datepicker('close');
    //     $('#selectday').val(event.date.format('yyyy-MM-dd'));
    //     $scope.queryList();
    // });

    $('#calendar').fullCalendar({
        header: {
            left: 'prev',
            center: 'title',
            right: 'next'
        },
        navLinks: true,
        contentHeight: 50,
        navLinkDayClick: function(date, jsEvent) {
            $scope.Oneday(date);
            //console.log('day', date.format('YYYY/MM/DD')); // date is a moment
        },
        eventClick: function(calEvent, jsEvent, view) {
            var strdate=calEvent.start._d;
            var day = moment(strdate);
            $scope.Oneday(day);
            //console.log('day',day.format('YYYY/MM/DD'));

        },
        eventAfterAllRender: function (view) {
            $(".fc-day-grid-container").height("25px");
        },
        defaultView: 'basicWeek',
        columnFormat: 'ddd M/D'
    });


    $('.fc-left').click(function () {
        var date = $('#calendar').fullCalendar('getDate');
        $scope.weekday(date);
        return false;
    });

    $('.fc-right').click(function () {
        var date = $('#calendar').fullCalendar('getDate');
        $scope.weekday(date);
        return false;
    });

    $scope.weekday = function(date) {
        $scope.startTime = date.format('YYYY-MM-DD 00:00:00');
        $scope.endTime = date.add(6, 'days').format('YYYY-MM-DD 23:59:59');
        $scope.queryList();
    };
    $scope.Oneday = function(date) {
        //$scope.startTime = date.format('YYYY-MM-DD 00:00:00');
        //$scope.endTime = date.format('YYYY-MM-DD 23:59:59');
        $scope.queryOneDay(date);
        //$scope.queryList();
    };

    //设置当前日期
    $('#calendar').fullCalendar('gotoDate', {
        defaultDate: moment()

    });

    $scope.queryList();
    //$scope.queryOneDay(moment());
});
